package com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.entity.BooksGoodsImg;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/11/11
 */
@Repository
public class BooksGoodsImgDao {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String deleteBooksGoodsImgSql = "DELETE FROM sw_books_goods_img WHERE seqNo=:seqNo and gdsseqno in (:gdsseqnos)";

    public void removeBySeqNoAndGdsseqnoIn(String seqNo, List<String> gdsseqnos) {
        Map<String, Object> params = new HashMap<>();
        params.put("seqNo", seqNo);
        params.put("gdsseqnos", gdsseqnos);
        namedParameterJdbcTemplate.update(this.deleteBooksGoodsImgSql, params);
    }

    private String insertBooksGoodsImgSql = "INSERT INTO sw_books_goods_img (seqNo, gdsseqno, img, storageTime)" +
            " VALUES (:seqNo, :gdsseqno, :img, :storageTime)";

    public void save(List<BooksGoodsImg> exgList) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(exgList);
        namedParameterJdbcTemplate.batchUpdate(insertBooksGoodsImgSql, batch);
    }

}
